package de.hs.rm;

import java.io.IOException;

import javax.management.InstanceAlreadyExistsException;
import javax.management.InstanceNotFoundException;
import javax.management.JMX;
import javax.management.MBeanException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerDelegateMBean;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import junit.framework.TestCase;

public class NotificationTest extends TestCase {
	
	private JMXConnector jmxConnector = null;
	private JMXServiceURL serviceUrl = null;
	private MBeanServerConnection mBeanServerConnection = null;
		
	protected void setUp() throws Exception {

		super.setUp();
		
		String host = "localhost";
        int port = 9999;
        String url = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi";
        serviceUrl = new JMXServiceURL(url);
		jmxConnector = JMXConnectorFactory.connect(serviceUrl, null);
	    mBeanServerConnection = jmxConnector.getMBeanServerConnection();
	}
	    
	
	public void testRegister()
	{
		ObjectName objName;
		try {
			objName = new ObjectName("de.hs.rm.jmx:type=HelloWorldTestCase");
			if(!mBeanServerConnection.isRegistered(objName))
			{
				System.out.println("Registering Mbean...");
				mBeanServerConnection.createMBean("de.hs.rm.jmx.HelloWorld", objName);
			}
		} catch (InstanceAlreadyExistsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MBeanRegistrationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotCompliantMBeanException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ReflectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MBeanException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedObjectNameException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	public void testUnregister()
	{
		ObjectName objName;
		try {
			objName = new ObjectName("de.hs.rm.jmx:type=HelloWorldTestCase");
			if(mBeanServerConnection.isRegistered(objName))
			{
				System.out.println("Unregistering Mbean...");
				mBeanServerConnection.unregisterMBean(objName);
			}
		} catch (MalformedObjectNameException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstanceNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MBeanRegistrationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	protected void tearDown() throws Exception {

		super.tearDown();

	}

}
